/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_04_restricted_samples.do
Date: October 2022

Description: Reproduces Table 4: Contribution of firm-embedded productivity, restricted samples.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"
	
*Log
cap log close
log using "${clogs}/table_04_restricted_samples.log",replace
*-------------------------------------------------------------------------------


*Notice that aggregates are only for countries with DA dummies in more than 10 sectors. A DA for a sector is calculated if at least 3 affiliates operate in that coutry-sector pair
local nn=10 /*minimum number of sectors allowed per year-isocode*/

foreach rsample in decile_2_9 decile_3_8 decile_4_7 decile_5_6 above3ctry above5ctry above10ctry {
display "`rsample'"
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent_`rsample'.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${data}/estimates_agg_naics_sales_s1_base_woparent_`rsample'.dta", clear
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
tempfile file_`rsample'
save `file_`rsample'', replace 
}



*Merging the different samples
*-----------------------------------------------------------------
clear all
use year isocode type D_A using "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear
rename D_A D_A_baseline

foreach rsample in decile_2_9 decile_3_8 decile_4_7 decile_5_6 above3ctry above5ctry above10ctry {
display "`rsample'"
merge 1:1 year isocode type using `file_`rsample'', keepusing(D_A*)
rename D_A D_A_`rsample'
drop _merge
}

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "P"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A_baseline D_A_decile_2_9 D_A_decile_3_8 D_A_decile_4_7 D_A_decile_5_6 D_A_above3ctry D_A_above5ctry D_A_above10ctry {
gen `var'_phi=(-1)*`var'*(0.2)
}


*------------------------------------------------------------------------------
*DA dummies (baseline)
*------------------------------------------------------------------------------
local LHS1 D_A_baseline_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c0: display %-03.2fc round(b[1,1],0.01)
global se0: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c0
display $se0

*------------------------------------------------------------------------------
*DA dummies for firms in the 2nd-9th deciles
*------------------------------------------------------------------------------
local LHS1 D_A_decile_2_9_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c1
display $se1

*------------------------------------------------------------------------------
*DA dummies for firms in the 3nd-8th deciles
*------------------------------------------------------------------------------
local LHS1 D_A_decile_3_8_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c2
display $se2

*------------------------------------------------------------------------------
*DA dummies for firms in the 4nd-7th deciles
*------------------------------------------------------------------------------
local LHS1 D_A_decile_4_7_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c3: display %-03.2fc round(b[1,1],0.01)
global se3: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c3
display $se3

*------------------------------------------------------------------------------
*DA dummies for firms in the 5nd-6th deciles
*------------------------------------------------------------------------------
local LHS1 D_A_decile_5_6_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c4: display %-03.2fc round(b[1,1],0.01)
global se4: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c4
display $se4

*------------------------------------------------------------------------------
*DA dummies for MNCs in 3 coountries or more 
*------------------------------------------------------------------------------
local LHS1 D_A_above3ctry_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c5: display %-03.2fc round(b[1,1],0.01)
global se5: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c5
display $se5

*------------------------------------------------------------------------------
*DA dummies for MNCs in 5 coountries or more 
*------------------------------------------------------------------------------
local LHS1 D_A_above5ctry_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c6: display %-03.2fc round(b[1,1],0.01)
global se6: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c6
display $se6

*------------------------------------------------------------------------------
*DA dummies for MNCs in 10 coountries or more 
*------------------------------------------------------------------------------
local LHS1 D_A_above10ctry_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c7: display %-03.2fc round(b[1,1],0.01)
global se7: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c7
display $se7


*------------------------------------------------------------------------------
*Generate Table 4 
*------------------------------------------------------------------------------
clear all
set obs 8

gen model=""
gen phi_share=. 
gen phi_share_se=. 

replace model="Baseline" if _n==1
replace phi_share=$c0 if _n==1
replace phi_share_se=$se0 if _n==1

replace model="MNEs (2nd-9th decile)" if _n==2
replace phi_share=$c1 if _n==2
replace phi_share_se=$se1 if _n==2

replace model="MNEs (3nd-8th decile)" if _n==3
replace phi_share=$c2 if _n==3
replace phi_share_se=$se2 if _n==3

replace model="MNEs (4nd-7th decile)" if _n==4
replace phi_share=$c3 if _n==4
replace phi_share_se=$se3 if _n==4

replace model="MNEs (5nd-6th decile)" if _n==5
replace phi_share=$c4 if _n==5
replace phi_share_se=$se4 if _n==5

replace model="MNEs (above3ctry)" if _n==6
replace phi_share=$c5 if _n==6
replace phi_share_se=$se5 if _n==6

replace model="MNEs (above5ctry)" if _n==7
replace phi_share=$c6 if _n==7
replace phi_share_se=$se6 if _n==7

replace model="MNEs (above10ctry)" if _n==8
replace phi_share=$c7 if _n==8
replace phi_share_se=$se7 if _n==8

export delimited using "${rmain}/table_04.csv", datafmt replace

log close


